Ext.namespace("SMeeting");

SMeeting.Edit = function(p) {
	this.parentWin=p;	
	this.userAdd = null;
	this.userUpdate = null;
	this.winMsg=null;
	this.current=null;
	
	this.txtRemind=new Ext.form.Hidden({name:'remind'});
	this.hdSuperviseId = new Ext.form.Hidden({name:'superviseId'});
	
	this.feedBackFile = new Ext.form.FileUploadField({
		fieldLabel : '会议纪要',
		emptyText: '请选择要上传的文件',
		width : 400,
		name : 'feedBackFileName',
		buttonText: '',
		buttonCfg: {
            iconCls: 'icon-compress'
        }
	});
	
	this.txtTitle = new Ext.form.TextField({
        fieldLabel: '会议名称',
        name:'superviseTitle',
        width: 180,
        allowBlank: false,
        blankText: '请输入会议名称!',
        maxLength:40
    });
    this.postName = new Ext.form.TextField({
        fieldLabel: '发文部门',
        name:'dispatchDeptName',
        width: 180,
        allowBlank: false,
        blankText: '请输入发文部门!',
        maxLength:40
    });
	this.getName = new Ext.form.ComboBox({
		width: 180,
		fieldLabel: '收文部门',
		name:'receiveDeptId',
        store: new Ext.data.ArrayStore({
        	fields: ['value', 'name'],
        	data: OMS.DEPT
        }),
        valueField: 'value',
        displayField: 'name',
        editable: false,
        mode: 'local',
        triggerAction: 'all'
	});	
    this.txtType = new Ext.form.ComboBox({
		width: 100,
		fieldLabel: '会议类别',
		name:'issueType',
        store: new Ext.data.ArrayStore({
        	fields: ['value', 'name'],
        	data: OMS.MEETING_TYPE2
        }),
        valueField: 'value',
        displayField: 'name',
        editable: false,
        mode: 'local',
        value : 0,
        triggerAction: 'all'
	});	
		this.txtNotion = new Ext.form.TextField({
        fieldLabel: '批示意见',
        name:'notion',
        width: 180,
        allowBlank: true,
        blankText: '请输入批示意见!',
        maxLength:40
    });
	
	this.dtDate = new Ext.form.DateField({
    	fieldLabel: '会议时间',
    	name:'issueDate',
		width: 100,
		format: 'Y-m-d'
	});
	
	this.form = new Ext.FormPanel({
		bodyStyle: 'padding: 5px 5px 0',
		frame :true,
		fileUpload: true, 
		autoScroll : true,
        labelWidth: 100,
        region:'north',	
        height: 140,
        labelAlign: 'right',
        items: [{   
        layout:'table',   //定义该元素为布局为列布局方式   
        border:false,   
        labelSeparator:'：', 
        layoutConfig : {columns:2},
		items:[
			{layout:'form',items:[this.txtType]},
			{layout:'form',items:[this.dtDate]},
			{layout:'form',items:[this.txtTitle]},
			{layout:'form',items:[this.txtNotion]},
			{layout:'form',items:[this.postName]},
			{layout:'form',items:[this.getName]},
			{colspan:2,layout:'form',items:[this.feedBackFile]}
			
		  
		] 

    }]   


    });

    this.store = new Ext.data.JsonStore({
	    url: '../superviseMeeting/listMeetingTask.form?timesnap=' + Date.parse(new Date()),
	    root: 'result',
	    totalProperty: 'total',
        remoteSort: true,
	    fields: ['auditId','superviseId','auditContent','auditorId','auditDate',
	    'auditState','inspectorId','inspectorName','dispatchDate','feedback','completeDate',
	    'auditorName','deptName','taskContent']
	});
	this.renderState = function(value){
		return OMS.SUPERVISE_STATE[value];
	};
	this.columns =  [
	    {header: '派发ID', sortable: true, align: 'right',width: 20,hidden:true,dataIndex: 'auditId'},
		{header: '督办ID', sortable: true, align: 'right',width: 20,hidden:true,dataIndex: 'superviseId'},
		{header: '派发意见', sortable: true, align: 'center',width: 20,hidden:true,dataIndex: 'auditContent'},
		{header: '派发人ID', sortable: true, align: 'center',width: 20,hidden:true,dataIndex: 'auditorId'},
		{header: '派发人', sortable: true, align: 'center',width: 20,hidden:true,dataIndex: 'auditorName'},
		{header: '阅处部门', sortable: true, align: 'center',width: 20,dataIndex: 'deptName'},
		{header: '任务派发时间', sortable: true, align: 'right',width: 20,hidden:true,dataIndex: 'dispatchDate'},
		{header: '反馈日期', sortable: true, align: 'right',width: 20,hidden:true,dataIndex: 'auditDate'},
		{header: '落实情况', sortable: true, align: 'center',width: 15,dataIndex: 'auditState',renderer:this.renderState},
		{header: '阅处人id', sortable: true, align: 'center',width: 20,hidden:true,dataIndex: 'inspectorId'},
		{header: '阅处人名称', sortable: true, align: 'center',width: 20,hidden:true,dataIndex: 'inspectorName'},		
		{header: '反馈情况', sortable: true, align: 'center',hidden:true,dataIndex: 'feedback'},
		{header: '完成期限', sortable: true, align: 'right',width: 20,hidden:true,dataIndex: 'completeDate'},
		{header: '要求', sortable: true, align: 'right',dataIndex: 'taskContent',
					editor: new Ext.form.TextArea({width: 200,height: 100})
			}
	];
	this.btnNew = new Ext.Button({
    	text: '添加',
    	iconCls: 'icon-new',
    	scope: this,
    	handler: this.onNew
    });
    this.btnRemove = new Ext.Button({
    	text: '删除',
    	iconCls: 'icon-remove',
    	scope: this,
    	handler: this.onRemove
    });
	this.grid = new Ext.grid.EditorGridPanel({
		title:'任务反馈情况',
		region: 'center',
		cm: new Ext.grid.ColumnModel(this.columns),
		sm: new Ext.grid.RowSelectionModel({singleSelect:false}),
		tbar: {items: [this.btnNew,this.btnRemove]},
		store:this.store,
		height: 240,
		autoScroll: true,
		clicksToEdit:1,
		viewConfig: { forceFit: true },
		scope:this,
        listeners:{ 
        	rowdblclick: function(g,index,event) {
        		var self = g.ownerCt;
      		  	self.onUpdate(g,index,event);
      		  }
       	},
        collapsible:true
	});	
    this.btnSave = new Ext.Button({
    	text:'提交阅处部门负责人',
    	handler:this.onSave,
    	scope: this
    });
	SMeeting.Edit.superclass.constructor.call(this, {
		title: '新建会议',
        width: 700,
        height: 420,
        resizable: false,
        modal: true,
        autoScroll: true,
        closeAction: 'hide',
        items: [this.form,this.grid],
        buttons: [this.btnSave,{
            text: '关闭',
            scope: this,
            handler: function() {
	        	this.hide();
        	}
        }],
        buttonAlign:'center'
	});
};
Ext.extend(SMeeting.Edit, Ext.Window, {
	initWin:function(){
		//alert(Ext.util.JSON.encode(data));
		this.form.getForm().reset();
		this.userSelector==null;
		this.grid.store.removeAll();			
	},
	saveDoc:function(){
		if(!this.form.getForm().isValid()){
			Utils.info2("有必填项没有填写，请检查！",180);
			return;
		}
  		var param={test:"1"};//this.form.getForm().getFieldValues();
  		var count=this.store.getCount();
  		if(count<=0){
  			alert("请选择阅处部门");
  			return false;
  		}
		for(var i=0 ;i<count;i++){
			var data = this.store.getAt(i).data;
			if(data.taskContent==undefined){
				alert("工作要求不能为空");
				return false;
			}
			param['grid_'+i]=Ext.util.JSON.encode(data);
		};
		param['fileName']=Ext.util.JSON.encode(this.feedBackFile.getValue());
		param['txtType']=this.txtType.getValue();
  		Utils.formatDate(param,'issueDate');
		scope=this,
		
		this.form.getForm().submit({
			waitTitle : '系统提示',
			waitMsg : '数据提交中，请稍候...',
			url:'../superviseMeeting/addDoc.form?timesnap=' + Date.parse(new Date()),
			method:'post',
		//Utils.request(this.addCon,{
			params:param,
			//scope:this,
			success:function(){

  				Utils.info2();
				this.scope.hide();
				this.scope.parentWin.queryDoc();
			}
		});
		
		Utils.info2();
		
		this.parentWin.queryDoc();
  		this.hide();
  		//alert(Ext.util.JSON.encode(param));

  		//alert(Ext.util.JSON.encode(param));		
		
	},
	onSave: function() {
		scope=this;
		if(this.winMsg==null)
			this.winMsg=new Message.Msg(function(){
				scope.txtRemind.setValue(scope.winMsg.txtRemind);
				scope.saveDoc();
			},'你确实要保存吗？');
		this.winMsg.show();
	},
	onNew: function(btn, event) {
		var scope=this;
		if(this.userAdd==null){
			var value=null;
			this.userAdd=new Tree2.User(value,function(o){
				scope.addUsers(o);
			},null,'115');
		}
		this.userAdd.show();
	},
	onRemove:function(btn,event){		
		var record = this.grid.getSelectionModel().getSelected();
		if(!record){
			Utils.warn2('请选择一条记录！',180);
			return false;
		}
        var scope=this;      
        Utils.confirm2({msg:"删除后将不可恢复，您确实要删除吗？",
        	width:220,
        	fn:function(){
		        	scope.store.remove(record);
		       }
	    });
	},
	onUpdate: function(g,index,event) {
		var scope=this;
		var rec = g.store.getAt(index);
    	this.current=rec;
		if(this.userUpdate==null){
			var value=rec.data['auditId'];
			this.userUpdate=new Tree.User(value,function(o){
				scope.setUsers(o);
			},null,'115');
		}
		this.userUpdate.show();
	},
	addUsers:function(o){
		for(var i=0;i<o.length;i++) {
			var value=o[i].value;
			if(!value)value=o[i].parentText;
			var data={auditorId:o[i].id,auditState:'1',deptName:value,auditorName:o[i].text};
			var rec = new this.grid.store.recordType(data);
	        this.grid.store.insert(0, rec);			
		}			
	},
	setUsers:function(o){
		if(o.length==null||o.length==0)
			return;
		var record=this.current; 
		this.current=null;
		var value=o[0].value;
			if(!value)value=o[0].parentText;
		record.set(['auditorId'],o[0].id);
	    record.set(['deptName'],value);
	    record.set(['auditorName'],o[0].text);
	}
});
